#include<bits/stdc++.h>
using namespace std;
const int MAXN = 101;
const int INF = 0xfffff;
int a[MAXN][MAXN];
int book[MAXN];
int dis[MAXN];
int n, v;
/**
 *
 * @param s
 */
void Dijkstra(int s) {
    for (int i = 1; i <= n; i++) {
        book[i] = 0;
        dis[i] = a[s][i];
    }
    book[s] = 1;///第一个点加入了确定最短路径的集合中
//    copy(dis + 1, dis + n + 1, ostream_iterator<int>(cerr, " "));
//       cerr << endl;
    for (int i = 1; i < n; i++) {
        ///找到离源点最近的点，加入到集合中
//      copy(dis + 1, dis + n + 1, ostream_iterator<int>(cerr, " "));
//       cerr << endl;
        int u = 1, MIN = INF;
        for (int j = 1; j <= n; j++) {
            if (book[j] == 0 && dis[j] < MIN) {/// 找dis数组中权值最小的点
                u = j;///待加入的点
                MIN = dis[j];
            }
        }
        book[u] = 1;
        for (int j = 1; j <= n; j++) {
            if (book[j] == 0 && a[u][j] < INF && dis[j] > dis[u] + a[u][j]) {
                dis[j] = dis[u] + a[u][j];
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        if (i == s)continue;
        if (dis[i] >= INF)cout << -1 << " ";
        else
            cout << dis[i] << " ";
    }
    cout << endl;
}
int main() {
    // freopen("A.in", "r", stdin);
    fill(book + 1, book + n + 1, 0);
    cin >> n >> v;
    ///初始化
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int t;
            cin >> t;
            if (i == j)a[i][j] = 0;
            else if (t == 0)a[i][j] = INF;
            else a[i][j] = t;
        }
    }  Dijkstra(v+1);

}